<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\service\position\PositionModule;
use Doraemon\service\statSign\Basic;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;

class CarouselBanner extends BaseModule
{

    const FORMAT_HIT = 'formatCarouselHit';

    /**
     * 推荐线顶部轮播api格式化输出
     * @param $hits
     * @param $uid
     * @param $userProfileExt
     * @param $statSignExt
     * @return array
     */
    public function format($hits, $uid, $userProfileExt, $statSignExt)
    {
        $banner_list = [];
        $response = [
            'type' => BlockType::TYPE_CAROUSEL_BANNER,
            'banner_list' => &$banner_list,
        ];
        $upgradeVersion = '5.21.0';
        switch ($this->position_id) {
            case 122:
                return $this->experience($hits);
        }
        if ($hits) {
            $i = 1;
            foreach ($hits as $hit) {
                //公共信息赋值
                $id = intval($hit['id']);
                $index = intval($i);
                $hit['pos'] = $index;
                switch ($hit['position_id']) {
                    case self::TAB3_ADD_WIKI_BANNER:
                        $banner_list[] = [
                            'id' => $id,
                            'pos' => $index,
                            'pic_id' => $hit['pic_id'],
                            'link' => $this->generateLink($hit['link_type'], $hit['link_params']),
                            'statSign' => $this->statSign($hit, $statSignExt),
                        ];
                        break;
                    default:
                        $row = [
                            'id' => $id,
                            'index' => $index,
                            'banner' => empty($hit['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                            'link' => $this->generateLink($hit['link_type'], $hit['link_params']),
                            'statType' => (string)'banner' . BlockType::TYPE_CAROUSEL_BANNER,
                            'statSign' => $this->statSign($hit, $statSignExt),
                        ];

                        $banner_list[] = $row;
                        break;
                }
                $i++;
            }
            $banner_list = \Doraemon\tools\StatSign::versionCompatible($banner_list, $upgradeVersion, BlockType::TYPE_BANNER, 'banner');
        }

        return $response;
    }

    private function experience($hits)
    {
        $response = [];
        if ($hits) {
            $pos = [];
            $map = array_column($hits, null, 'id');
            foreach ($hits as $hit) {
                $pos[$hit['ext_type']][$hit['id']] = $hit;
            }
            foreach ($pos as $list) {
                shuffle($list);
                $pop = array_pop($list);
                $response[] = $pop;
                unset($map[$pop['id']]);
            }
            shuffle($map);
            $response  = array_merge($response, array_slice($map, 0, 3));
        }
        $format = [];
        $i = 1;
        foreach ($response as $v) {
            $statSignExt = [
                Basic::EXTEND_KEY_CONFIG_ID => $v['id'],
                Basic::EXTEND_KEY_INDEX => $i,
            ];
            $i++;
            $statSign = $this->statSign($v, $statSignExt);
            $format[] = [
                "type" => BlockType::TYPE_BANNER,
                "banner" => [
                    "title"    => $v['title'],
                    "pic_url"  => empty($v['pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $v['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                    "link"     => $this->generateLink($v['link_type'], $v['link_params']),
                ],
                "statSign" => $statSign,
            ];
        }
        return $format;
    }

}